import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import AES from "@/AES";
import {
  Dialog
} from 'vant';

Vue.use(Router);


const router = new Router({
  mode: "history",
  routes: [{
      path: "/",
      redirect: "/login"
    },
    {
      path: "/login",
      component: () => import("@/components/Phone/Login"),
    },
    {
      path: "/client",
      name: "client",
      meta: {
        usertype: 0
      },
      component: () => import("@/components/Phone/Main"),
      children: [{
          path: "home",
          name: "home",
          meta: {
            title: "主页",
            usertype: 0
          },
          component: () => import("@/components/Phone/Home")
        },

        {
          path: "my",
          name: "my",
          meta: {
            title: "个人管理",
            usertype: 0
          },
          component: () => import("@/components/Phone/Personal/My"),
          children: [{
              path: "person_home",
              name: "person_home",
              meta: {
                title: "个人功能页",
                usertype: 0
              },
              component: () => import("@/components/Phone/Personal/PersonHome"),
            },

          ]
        }
      ]
    },
    {
      path: "/idcard",
      name: "idcard",
      meta: {
        title: "实名认证",
        usertype: 0
      },
      component: () => import("@/components/Phone/Personal/IdCard"),
    },
    {
      path: "/faceadd",
      name: "faceadd",
      meta: {
        title: "人脸录入",
        usertype: 0
      },
      component: () => import("@/components/Phone/Personal/FaceAdd"),
    },
    {
      path: "/qrcode",
      name: "qrcode",
      meta: {
        title: "扫码登记",
        usertype: 0
      },
      component: () => import("@/components/Phone/Personal/Qrcode"),
    },
    {
      path: "/404",
      name: "404",
      meta: {
        title: "404",
        usertype: 0
      },
      component: () => import("@/components/Phone/404"),
    },
    {
      path: "/charge",
      name: "charge",
      meta: {
        title: "充值",
        usertype: 0
      },
      component: () => import("@/components/Phone/Personal/Charge"),
    }
  ]
});

// 路由前置守卫
router.beforeEach((to, from, next) => {
  const allow_path = ["/404", "/login","/idcard","/charge"];
  if (allow_path.indexOf(to.path) != -1) {
    next();
  // } else if (to.path == "/idcard") {
  //   if (from.path == "/login") {
  //     next();
  //   } else {
  //     next(from.path);
  //   }
  // } else if (to.path == "/faceadd") {
  //   if (from.path == ("/idcard" || "/client/home")) {
  //     next();
  //   } else {
  //     next(from.path);
  //   }
  // } else if (to.path == "/qrcode") {
  //   if (from.path == "/faceadd") {
  //     next();
  //   } else {
  //     next(from.path);
  //   }
  } else {
    let token = localStorage.getItem("Authorization");
    let usertype = parseInt(AES.decrypt(store.state.usertype));
    let is_active = AES.decrypt(store.state.is_active);
    if (token === null || token === "") {
      next("/login");
    } else {
      if (usertype >= to.meta.usertype && is_active == "True") {
        next();
      } else {
        Dialog.alert({
          title: '提示',
          message: '账号权限不足或者账号未激活，无法进入此页面'
        })
        next("/404?msg=账号权限不足或者账号未激活，无法进入此页面");
      }
    }
  }
});

export default router;
